<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" @selection-change="e => selection = e" ref="SuperTablePage">
      <template v-slot:action-right>
        <el-upload ref="upload" :show-file-list="false" action="/oms-api/purchase/OmsPurchaseSupplierRefRpc/importExcel.json"
          v-auth="'oms_return_good_confirm_import'" style="display: inline;" :on-success="uploadExcelSuccess"
          :on-error="uploadExcelError">
          <el-button slot="trigger" style="margin-left: 10px;" size="mini">{{ $tt('label_import') }}</el-button>
        </el-upload>
      </template>
    </ex_super_table_page>


  </div>
</template>
<script>
import superTablePageFunction from "@/mixin/super-table-page-function";
import page_table_model_manage from "@/mixin/page_table_model_manage";
import emitOnFunction from "@/mixin/emit-on-function";
let emitKey = "oms>purchase>supplyManage>supply_relationship_manage";

import select_file_temp from "@/component/base/select_file_temp";

export default {
  title: "label_purchase_commodity_sku_relationship",
  mixins: [
    superTablePageFunction,
    emitOnFunction(emitKey),
    page_table_model_manage,
  ],
  components: {},
  data() {
    return {
      option: {
        search: {
          default: { skuStatus: "ENABLE" },
          // resetFieldFun: (def, org) => { def.skuStatus = org.skuStatus; return def },
          // formDataHandle: (data) => {
          //   if (data.categIds && data.categIds.length > 0) {
          //     data.categId = data.categIds[data.categIds.length - 1]
          //   } else {
          //     data.categId = null
          //   }
          //   return data
          // },
          model: [
            { label: "label_keyword", key: "keyword", type: 'input', attrs: { placeholder: `${this.$tt('label_name')} / ${this.$tt('label_code')} / ${this.$tt('label_barcode')}`, }, },
            { label: "label_customer", key: "customerId", type: 'common_customer_choose' },
          ],
        },
        actionExtra: [
          {
            label: "label_export",
            auth: "oms_purchase_supplier_edit",
            click: this.exportExcel,
          },
          {
            // label: 'label_download_import_template',
            auth: "oms_purchase_supplier_edit",
            render: (h, { data, item }) => {
              return h(select_file_temp, {
                props: {
                  service: "OMS",
                  module: "PURCHASE_SUPPLIER_REF",
                  label: this.$tt("label_download_import_template"),
                },
              });
            },
          },
        ],

        table: {
          url: "/mms-api/sku/MmsSkuInfoRpc/selectPage.json",
          attrs: {},
          model: this.init_table_model(),
        },
      },
    };
  },
  methods: {
    option_table_model() {
      return [
        {
          type: "selection",
          clickToggle: true,
          keyGroup: "label_table_operate",
        },
        // { key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 180, },
        { key: 'skuBarcode', label: 'label_sku_code', 'min-width': 150, },
        { key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 180, },
        { key: 'skuName', label: 'label_sku_name', 'min-width': 130, 'show-overflow-tooltip': true, },
        { key: 'skuNameEn', label: 'label_sku_name_en', 'min-width': 130, 'show-overflow-tooltip': true, },
        { key: 'num', label: 'label_supplier_name', 'min-width': 130, 'show-overflow-tooltip': true, },
        {
          key: 'skuDeclaredValue', label: 'label_declared_value', 'width': 140, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$formatNameCode(v, r.currencyCode)
        },
        {
          type: "operate",
          width: 150,
          operate: [
            {
              label: "label_detail",
              // auth: 'oms_purchase_supplier_edit',
              // if: this.isEditShow,
              click: (row) => {
                this.$router.push({
                  path: "/page/oms/purchase/supplyManage/commodity_sku_relationship_set",
                  query: { emitKey, id: row.id, customerId: row.customerId, skuOnlyCode: row.skuOnlyCode },
                });
              },
            },
          ],
        },
      ];
    },
    uploadExcelSuccess(response, file, fileList) {
      if (response.code == 0) {
        this.$message.success(this.$tt('label_upload_success'));
        this.$emitPage("oms>import_manage", 'reload')
        this.$confirm(this.$tt("tip_importing_confirm_goto_recode_list"), this.$tt('label_tip'), {
          type: 'success'
        }).then(() => {
          this.$router.push({
            path: '/page/oms/report/import_manage',
          });
        }).catch(() => {

        });
      } else {
        this.$message.error(response.message || this.$tt('label_upload_fail'));
      }
      if (this.$refs.upload) this.$refs.upload.clearFiles()
    },
    uploadExcelError(response, file, fileList) {
      this.$message.error(this.$tt('label_upload_error'))
    },
    exportExcel() {
      const searchParams = this.$refs.SuperTablePage.getSearchData()
      console.log(this.selection);
      let idList = []
      if (this.selection) {
        idList = this.selection.map(item => item.id)
      }
      let params = {...searchParams,idList }
      this.$functionCommon({
        url: "/oms-api/purchase/OmsPurchaseSupplierRpc/exportExcel.json",
        data: params, type: 'form',
      })
    },
  },
};
</script>
<style></style>
